function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

// TODO: 点击推出按钮时执行的函数
function logout() {
    $.ajax({
        url: "http://127.0.0.1:5000/api/v1.0/sessions/",
        type: "delete",
        headers: {
            "X-CSRFToken": getCookie("csrf_token")
        },
        dataType: "json",
        success: function (resp) {
            if ("0" == resp.errno) {
                alert('退出登录成功')
                location.href = "/index.html";
            }
        }
    });
}

$(document).ready(function(){

    // TODO: 在页面加载完毕之后去加载个人信息
    $.get('http://127.0.0.1:5000/api/v1.0/users/', function (resp) {
        if (resp.errno == "0") {
            $("#user-name").html(resp.data.name)
            $("#user-mobile").html(resp.data.mobile)
            if (resp.data.avatar) {
                $("#user-avatar").attr('src', resp.data.avatar)
            }
        }else {
            location.href = "/login.html"
        }
    }, 'json')

});
